
import requests
from loguru import logger

from basic.base_login import TOKEN
from config import ENV

def request_hooks(response,*args,**kwargs):
    logger.debug(f"接口响应结果:{response.text}")

class BaseRequest:
    def __init__(self):
        self._session =requests.Session()
        self._session.headers.update({"Content-Type":"application/json","token":TOKEN})
        self._session.hooks["response"].append(request_hooks)
        self._requests =self._session

    def _post(self,api_name,url,data):
        base_url=ENV.HOST+url
        logger.debug(f"接口_{api_name,}_请求参数:{base_url}")
        return  self._requests.post(f"{base_url}",json=data).json()
    def _get(self,api_name,url,params=None):
        base_url = ENV.HOST + url
        logger.debug(f"接口_{api_name,}_请求参数:{base_url}")
        return  self._requests.get(f"{base_url}",params=params).json()
    def _put(self,api_name,url,data):
        base_url = ENV.HOST + url
        logger.debug(f"接口_{api_name,}_请求参数:{base_url}")
        return self._requests.put(f"{base_url}", json=data).json()
    def _delete(self,api_name,url,params=None):
        base_url = ENV.HOST + url
        logger.debug(f"接口_{api_name,}_请求参数:{base_url}")
        return self._requests.delete(f"{base_url}", params=params).json()

# class BaseLogin(BaseRequest):
#     # def __init__(self):
#     #     data = {
#     #         "method" : "GET",
#     #         "url" : "http://124.70.39.201/adminapi/login",
#     #         "params" : {"username" : "admin", "password" : "Aa123456!"},
#     #     }
#     #     r = requests.request(**data)
#     #     self.token = r.json()["data"]["token"]
#     def login(self):
#         r= self._get("http://124.70.39.201/adminapi/login",{"username" : "admin", "password" : "Aa123456!"})
#         print(r.text)
# if __name__ == '__main__':
#     BaseLogin().login()
